﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using PicHunter.Logging;

namespace PicHunter
{
    public partial class NewForm : Form
    {
        Log LOG = Log.Instance;

        public String URL
        {
            get
            {
                return txtUrl.Text;
            }
        }

        public String NAME
        {
            get
            {
                return txtName.Text;
            }
        }

        public NewForm()
        {
            InitializeComponent();
            String subjectString = Clipboard.GetText();
            bool foundMatch = false;
            try
            {
                foundMatch = Regex.IsMatch(subjectString, @"\A(?:((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+))\Z");
            }
            catch (ArgumentException ex)
            {
                LOG.Error(ex);
            }
            if (foundMatch)
            {
                txtUrl.Text = subjectString;
            }
        }

        private void txtUrl_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                //int x = Int32.Parse(txtUrl.Text);
                errorProvider1.SetError(txtUrl, "");
            }
            catch (Exception)
            {
                errorProvider1.SetError(txtUrl, "Invalid url.");
            }

        }

        private void btnGenName_Click(object sender, EventArgs e)
        {
            if (URL != null)
            {
                int start = URL.LastIndexOf("/") + 1;
                int end = URL.LastIndexOf(".");
                if (start > 0 && end > start)
                {
                    txtName.Text = URL.Substring(start, end - start);
                }
            }
        }
    }
}
